package com.wjjm.spoonselling.controller;

import com.wjjm.spoonselling.pojo.Customer;
import com.wjjm.spoonselling.service.CustomerService;
import com.wjjm.spoonselling.util.Code;
import com.wjjm.spoonselling.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @PostMapping("/login")
    public Result login(@RequestBody Customer customer, HttpSession session) {
        Result result = customerService.login(customer);
        return result;
    }

    @PostMapping("/register")
    public Result register(@RequestBody Customer customer){
        return customerService.register(customer);
    }

    @GetMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable String name){
        Customer customer = customerService.selectByName(name);
        int code = customer != null ? Code.SELECT_OK:Code.SELECT_ERR;
        String msg = customer != null ? "数据查询成功":"数据查询失败，请重试";
        return new Result(code,customer,msg);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Customer customer){
        boolean flag = customerService.update(customer);
        int code = flag == true ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg = flag == true ? "数据更新成功":"数据更新失败，请重试";
        return new Result(code,flag,msg);
    }

    @PutMapping("/updateP")
    public Result updateP(@RequestBody Customer customer){
        boolean flag = customerService.updatePassword(customer);
        int code = flag == true ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg = flag == true ? "密码修改":"密码修改失败，请重试";
        return new Result(code,flag,msg);
    }
}
